package com.study.d016event;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/** 鼠标事件
*@description
*@author xtayhame
*date 2020年3月7日
*version
*since v1.0
*/
public class MouseEventMain extends Application{
	public static void main(String[] args) {
		launch(args);

	}
	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		HBox hBox1 = new HBox();

		Button b1 = new Button("button1");
		Button b2 = new Button("button2");

		HBox hBox2 = new HBox(50);
		Button drag1 = new Button("拖拽1");
		Button drag2 = new Button("拖拽2");

		hBox1.getChildren().addAll(b1,b2);
		hBox2.getChildren().addAll(drag1,drag2);

		anchorPane.getChildren().addAll(hBox1,hBox2);
		AnchorPane.setTopAnchor(hBox1, 100.0);
		AnchorPane.setLeftAnchor(hBox1, 100.0);

		AnchorPane.setTopAnchor(hBox2, 200.0);
		AnchorPane.setLeftAnchor(hBox2, 100.0);

		stage.setScene(new Scene(anchorPane, 400, 500));
		stage.show();

		//鼠标点击事件，包括左键，右键，中间键
		b1.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				/*System.out.println("相对button自身的坐标: X="+event.getX() + "  Y=" + event.getY());
				System.out.println("相对Scene的坐标: X="+event.getSceneX() + "  Y=" + event.getSceneY());
				System.out.println("相对电脑屏幕的坐标: X="+event.getScreenX() + "  Y=" + event.getScreenY());*/

				if(event.getButton()==MouseButton.PRIMARY && event.getClickCount()==2){
					System.out.println("鼠标左键被点击2次.");
				}else if(event.getButton()==MouseButton.SECONDARY){
					System.out.println("鼠标右键被点击");
				}else if(event.getButton()==MouseButton.MIDDLE){
					System.out.println("鼠标中间键被点击");
				}
				System.out.println("鼠标点击.");
				//判断两次点击的位置是否是统一位置，如果是则说明没有移动；在判断拖拽时有用
				boolean still=event.isStillSincePress();
				System.out.println("是否点击是的是同一个位置：" +still);
			}
		});

		b1.setOnMouseEntered(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("鼠标进入.");
			}
		});

		b1.setOnMouseExited(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				System.out.println("鼠标退出.");
			}
		});

		b1.setOnMousePressed(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("鼠标按下.");
				//可以在鼠标按下时，触发拖拽监测方法
				//event.setDragDetect(true);
			}
		});

		b1.setOnMouseReleased(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("鼠标释放.");
			}
		});


		System.out.println("********************************************鼠标拖拽事件********************************************");
		//实现按钮的拖拽
		drag1.setOnMouseDragged(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("拖拽按钮1===>setOnMouseDragged 开始拖拽");
				boolean still=event.isStillSincePress();
				System.out.println("是否点击是的是同一个位置：" +still);
			}
		});

		drag1.setOnMouseDragEntered(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("拖拽按钮1===>setOnMouseDragEntered 进入拖拽");
			}
		});

		//拖拽监测
		drag1.setOnMouseDragReleased(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("拖拽按钮1===>setOnMouseDragReleased 拖拽释放");

			}
		});

		drag1.setOnMouseDragExited(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				System.out.println("拖拽按钮1===>setOnMouseDragExited 拖拽退出");
			}
		});


		drag1.setOnDragDetected(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				//触发所有
				drag1.startFullDrag();
				System.out.println("拖拽按钮1===>setOnDragDetected 触发所有");
			}

		});

		//只在按钮内部拖拽有效，且一定要在setOnDragDetected() 方法中调用startFullDrag()方法
		drag1.setOnMouseDragOver(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				System.out.println("拖拽按钮1===>setOnMouseDragOver 全部拖拽");
			}
		});

	}
}
